import {getWy,getPlatform} from '@/uni_modules/wy-core/context'
const that={
	async kh_uploadFile(obj){
		var wy=getWy()
	 	let {_group,uploadTask,onUploadProgress,ext}=obj
	 	let model={
	 		title:'上传中...',
	 		success:null,
	 		...obj
	 	}
	 	let {title,success}=model
	 	let {filePath,atc}=obj.data
		if(!atc){
			atc='common'
		}
		if(!ext){
			var sps=filePath.split('.')
			if(sps.length>1){
				ext=sps[sps.length-1]
			}
			if(!ext){
				ext='png'
			}
		}
		var t = wy.pubfn.unix();
		var time = wy.pubfn.formatTime(t,'YYYYMMDD');
		var fileName=atc+'/'+time+'/'+wy.pubfn.md5(wy.pubfn.uuid())+'.'+ext

		var uploadAPI=wy.formatApiUrl(_group,{ac:'UploadImage',atc})
		console.log(fileName)
	 	return new Promise((resolve, reject)=>{
			uniCloud.uploadFile({
					filePath: filePath,
					cloudPath: fileName,
					onUploadProgress(progressEvent) {
					  if(onUploadProgress){
						 var percentCompleted = Math.round(
							 (progressEvent.loaded * 100) / progressEvent.total
						   );
						  onUploadProgress({
						  		progress:percentCompleted
						  })

					  }

					},
					success(res) {
						let {fileID}=res
						if(success){
							success({fileID,...res})
						}
						resolve({url:fileID,file:{id:1}})
					},
					fail(e) {
						reject(e)
					}
				});
	 	})
	 },
	async kh_uploadFile1(obj){
		var wy=getWy()
	 	let {url,_group,uploadTask}=obj
	 	let model={
	 		title:'上传中...',
	 		success:null,
	 		...obj
	 	}
	 	let {title,success}=model
	 	let {filePath,atc}=obj.data
		if(!atc){
			atc='common'
		}
		var uploadAPI=wy.formatApiUrl(_group,{ac:'UploadImage',atc})
		console.log(uploadAPI)
	 	return new Promise((resolve, reject)=>{
			var task =uni.uploadFile({
				url:uploadAPI,
				filePath:filePath,
				name:'file',
				fail(e) {
					reject(e)
				},
				success(e) {

					let res=JSON.parse(e['data'])
					let {ret,data}=res
					if(ret==200)
					{
						if(data.data){
							data=data.data
						}
						if(success){
							success({fileID:data.url,...data})
						}
						resolve({
							fileID:data.url,
							...data
						})
					}
					else{
						reject(res)
					}
				},
				complete() {
					if(title){
						uni.hideLoading()
					}
				}

			})
			if(uploadTask&&wy.pubfn.isFunction(uploadTask)){
				uploadTask(task)
			}
	 	})
	 }
};
export default that
